package com.aaa.marketresearch.android.app;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import com.aaa.marketresearch.android.data.UserInfo;
import com.aaa.marketresearch.android.service.HttpClient;
import com.aaa.marketresearch.android.util.SimpleLog;
import com.aaa.marketresearch.android.util.connector.IDataTextResponse;


public class LoginScreen extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_screen);
    }

    @Override
    protected void onResume() {
        super.onResume();
        UserInfo userInfo = UserInfo.getInstance();
        userInfo.loadData(this);
        ((EditText) findViewById(R.id.accountField)).setText(userInfo.getAccount());
        ((EditText) findViewById(R.id.passwordField)).setText(userInfo.getPassword());

        Spinner spinnerServer = (Spinner) findViewById(R.id.serverField);
        ArrayAdapter<CharSequence> adapterGender = ArrayAdapter.createFromResource(this, R.array.server_Array, android.R.layout.simple_spinner_item);
        adapterGender.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerServer.setAdapter(adapterGender);

        int spinnerServerPos = adapterGender.getPosition(userInfo.getServer());
        spinnerServer.setSelection(spinnerServerPos);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.login_screen, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            SimpleLog.info("settings click");
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void loginButtonClick(View view) {
        SimpleLog.info("loginButtonClick");

        String account = ((EditText) findViewById(R.id.accountField)).getText().toString();
        String password = ((EditText) findViewById(R.id.passwordField)).getText().toString();

        UserInfo.getInstance().setAccount(account);
        UserInfo.getInstance().setPassword(password);

        Spinner spinnerServer = (Spinner) findViewById(R.id.serverField);
        String server = spinnerServer.getSelectedItem().toString();
        UserInfo.getInstance().setServer(server);


        SimpleLog.info("account: " + account + " password: " + password);

        HttpClient httpClient = new HttpClient(this);
        httpClient.login(new IDataTextResponse() {
            @Override
            public void onTextResponse(String text) {
                if (!UserInfo.getInstance().isAuthorized()) {
                    SimpleLog.info("wrong password");
                    AlertDialog.Builder alertDialog = new AlertDialog.Builder(LoginScreen.this);
                    alertDialog.setMessage("Авторизация не удалась");
                    alertDialog.setPositiveButton("Ok",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                }
                            }
                    );
                    alertDialog.show();
                } else {
                    SimpleLog.info("login as " + UserInfo.getInstance().getRole());
                    UserInfo.getInstance().saveData(LoginScreen.this);
                    Intent intent = new Intent(LoginScreen.this.getApplicationContext(), MainMenu.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                    startActivity(intent);
                }
            }

            @Override
            public void onError() {
                SimpleLog.info("error");
            }
        });
    }

}
